<?php

namespace plugin\webman\sports;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\common\Common;


class Method3
{

  static function npcinfo(){
        $data=[
            ["id"=>"1","name"=>"海盗猿"],
            ["id"=>"2","name"=>"红发猿"],
            ["id"=>"3","name"=>"骑士猿"],
            [ "id"=>"4","name"=>"兔头猿"],
            [ "id"=>"5","name"=>"船长猿"],
            [ "id"=>"6","name"=>"牛仔猿"],
        ];
       return $data;
  }
  static function getnpcname($id){
      $data=self::npcinfo();
      foreach ($data as $key => $value) {
          if($value['id']==$id){
              return $value['name'];
          }
      }
  }
  static function getnpcid($name){
      $data=self::npcinfo();
      foreach ($data as $key => $value) {
          if($value['name']==$name){
              return $value['id'];
          }
      }
  }

//   获取运动员近100期冠军次数
  static function get_npc_win_num(){
      $expect = time()-60*100;
      $data=self::npcinfo();
      $win_num=array();
      $list = [];
      foreach ($data as $key => $value){
        $value['mingci'] = Db::table("wa_sports_paiming")->where("npcid",$value['id'])->where("mingci",1)->count();
       
        $list[]=$value;
      }
      
      return $list;
  }

  // 随机生成不重复的npc 1-6名次

  static function getmingci(){
      $expect = Common::getexpect();
      $data=self::npcinfo();
      $list = [];
      $mingci=array();
      foreach ($data as $key => $value){
        $mingci[]=$value['id'];
      }
      $mingci=array_unique($mingci);
      shuffle($mingci);
      $mingci=array_slice($mingci,0,6);
      foreach ($mingci as $key => $value){
        $a['id']=$value;
        $a['name']=self::getnpcname($value);
        $a['mingci']=$key+1;
        $list[]=$a;
      }
      // 入库记录名次
      foreach ($list as $key => $value){
        $record = [
          'npcid'=>$value['id'],
          'npcname'=>$value['name'],
          'mingci'=>$value['mingci'],
          'expect'=>$expect,
          'addtime'=>time()
        ];
        Db::table("wa_sports_paiming")->insert(
          $record 
        );
      }
      $arr=json_encode($list);
      Common::setRedis('mingci_'.$expect,$arr);
      return $list;
  }

  // 获取缓存中的mingci_expect

  static function getmingci_expect(){
      $expect = Common::getexpect();
      $arr2=Common::getRedis('mingci_'.$expect);
      if(!$arr2){
        $arr=self::npcinfo();
        foreach ($arr as $key => $value){
          $value['mingci']=0;
          $arr2[]=$value;
        }
      }else{
        $arr2 = json_decode($arr2,true);
      }
      
      return $arr2;
  }

  // 中奖后的更改结果 type: 1中奖  0未中奖
  static function zhongjiang($id,$guanjun,$type,$reward=''){
    if($type==1){
      $up = Db::table("wa_sports_touzhu")->where('id',$id)->update(['ifwin'=>1,'result'=>$guanjun['id'],'result_name'=>$guanjun['name'],'reward'=>$reward]);
      // 返还积分
     
    }else{
      $up = Db::table("wa_sports_touzhu")->where('id',$id)->update(['ifwin'=>0,'result'=>$guanjun['id'],'result_name'=>$guanjun['name']]);
    }
    
    if($up){
      return true;
    }else{
      return false;
    }
    
  }



}